package com.itjob.scott.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.itjob.scott.action.UserAction;

public class UserServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private WebApplicationContext context=null;
	private UserAction action = null;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		ServletContext application= config.getServletContext();
		context = WebApplicationContextUtils.getWebApplicationContext(application);
		action = context.getBean(UserAction.class);
		
	}


	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String username= request.getParameter("username");
		String password= request.getParameter("password");
		
		boolean b = action.login(username, password);
		
		request.getSession().setAttribute("username", username);
		
		if(b){
			response.sendRedirect("success.jsp");
		}else{
			response.sendRedirect("error.jsp");
		}
	}

}
